package com.dut.bbs.adapter;

import java.util.Map;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.dut.bbs.activity.LoginActivity;
import com.dut.bbs.data.UserInfo;
import com.dut.bbs.entity.LoginEntity;
import com.dut.bbs.util.NetWorkAvailable;
import com.dut.bbs.util.URLConstant;

class ThreadViewHolder {
	public TextView tv_title;
	public TextView tv_content;
	public TextView tv_reply;
	public TextView tv_id;
	public TextView tv_date;
	public Map<String, String> dataMap;
	public Context context;
	Handler handler;
	public ProgressDialog progressDialog;
	public ThreadViewHolder(Map<String, String> dataMap, final Context context,Handler handler,ProgressDialog progressDialog){
		this.dataMap = dataMap;
		this.context = context;
		this.handler = handler;
		this.progressDialog = progressDialog;
		progressDialog.setTitle("请稍等");
		progressDialog.setMessage("抓取页面中...");
	}
	public void init(final String id){
		tv_id.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				progressDialog.show();
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						String info = UserInfo.getInfo((String) tv_id.getText());
						Message msg = new Message();
						msg.what = 2;
						Bundle bundle = new Bundle();
						bundle.putString("info", info);
						bundle.putString("id", id);
						bundle.putInt("flag", 3);
						msg.setData(bundle);
						handler.sendMessage(msg);
					}
				}).run();
			}
		});
		tv_reply.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!LoginEntity.isLogin()) {
					AlertDialog.Builder builder = new Builder(context);
					builder.setMessage("亲，你还没登录呢");
					builder.setTitle("提示");
					builder.setIcon(android.R.drawable.ic_dialog_alert);
					builder.setPositiveButton("去登录",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									Intent intent = new Intent(context,LoginActivity.class);
									Bundle bundle = new Bundle();
									bundle.putString("fromItem", "1");
									intent.putExtras(bundle);
									context.startActivity(intent);
								}

							});
					builder.setNegativeButton(
							"算了吧",
							new android.content.DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							});
					builder.create().show();
				}else{
					progressDialog.show();
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							if(!NetWorkAvailable.goodNetIp())
								handler.sendEmptyMessage(3);
							else{
								Message msg = new Message();
								msg.what = 4;
								Bundle bundle = new Bundle();
								bundle.putString("url", dataMap.get("reply"));
								bundle.putString("title", dataMap.get("topic"));
								bundle.putString("id", id);
								bundle.putInt("flag", 2);
								msg.setData(bundle);
								handler.sendMessage(msg);
							}
						}
					}).run();
				}
			}
		});
	}
}
